import time
import random
from fake_useragent import UserAgent
import requests
import configparser


class RequestsOp(object):
    '''
    requests库
    '''
    uat = UserAgent()

    @classmethod
    def get(cls, url, ua=True):
        headers = {'User-Agent': cls.uat.random}
        return requests.get(url=url, headers=headers if ua else None, verify=False)


class MyUtils(object):
    @classmethod
    def get_ruid(cls):
        return str(hex(int(round(time.time() * 1000)) + random.randint(0, 999)))[2:]

    @classmethod
    def get_file_type(cls, ext):
        if not ext:
            return None
        if ext in ["bmp", "gif", "jpg", "jpeg", "png", "tif", "tiff"]:
            return 'img'
        elif ext in ["wav", "aif", "asf", "au", "mp3", "wma", "aac", "ogg", "ape", "flac"]:
            return 'audio'
        elif ext in ["txt", "pdf", "doc", "docx", "xls", "xlsx", "pptx", "ppt"]:
            return 'doc'
        elif ext in ["mp4", "avi", "rmvb", "rm", "mov", "mpg", "3gp", "mkv", "dat", "vob", "wmv", "flv"]:
            return 'video'
        elif ext in ["iso", ]:
            return 'iso'
        else:
            return 'file'

    @classmethod
    def get_stander_size(cls, size, unit):
        if size:
            print('size: {0}'.format(size))
            print('unit: {0}'.format(unit))
            if isinstance(size, str) and unit == '已包含单位':
                for KB in ['KB', 'kb', 'Kb', 'kB', 'k', 'K']:
                    if KB in size:
                        size = size.replace(KB, '')
                        return int(float(size)) * 1024
                for MB in ['MB', 'mb', 'Mb', 'mB', 'm', 'M']:
                    if MB in size:
                        size = size.replace(MB, '')
                        return int(float(size)) * 1024 * 1024
                for GB in ['GB', 'gb', 'Gb', 'gB', 'g', 'G']:
                    if GB in size:
                        size = size.replace(MB, '')
                        return int(float(size)) * 1024 * 1024 * 1024
                for B in ['Byte', 'BYTE', 'byte', '字节', 'B']:
                    if B in size:
                        size = size.replace(B, '')
                        return int(float(size))
                for b in ['bit', '比特', 'b']:
                    if b in size:
                        size = size.replace(b, '')
                        return int(int(float(size)) / 8)

            if unit != '已包含单位':
                if not isinstance(size, int):
                    size = int(float(size))
                if unit == 'BYTE':
                    return size
                if unit == 'KB':
                    return size * 1024
                if unit == 'MB':
                    return size * 1024 * 1024
                if unit == 'GB':
                    return size * 1024 * 1024 * 1024

        return None


class CfOp(object):
    '''
    读写配置文件类
    '''
    def __init__(self, init_file_path):
        self.init_file_path = init_file_path

    # 读写配置文件 四连 start -----------------------------------------------
    def get_init_file_cf(self):
        '''
        读配置文件
        :return:
        '''
        cf = configparser.ConfigParser()
        # print(qApp.cx_project_root + r'/init_file/init/es_db_init.ini')
        cf.read(self.init_file_path, encoding="utf-8-sig")
        return cf

    def init_write(self, cf):
        '''
        写配置文件
        :param cf:
        :return:
        '''
        cf.write(open(self.init_file_path, "w", encoding="utf-8-sig"))

    def get_option(self, sec, opt):
        '''
        获取 option 如果为空返回 None
        :param sec:
        :param opt:
        :return:
        '''
        option = None
        cf = self.get_init_file_cf()
        try:
            option = cf.get(sec, opt)
        except Exception as e:
            pass
        return option

    def save_option(self, sec, opt, value):
        '''
        保存 option
        :param sec:
        :param opt:
        :return:
        '''
        cf = self.get_init_file_cf()
        try:
            cf.get(sec, opt)
            cf.remove_option(sec, opt)
        except Exception as e:
            pass
        cf.set(sec, opt, str(value).replace('\'', '"'))
        self.init_write(cf)
    # 读写配置文件 四连 end -----------------------------------------------


if __name__ == '__main__':
    print(MyUtils.get_file_type('aif'))